<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
// Note: This test is only testing synthetic DomKey for eventSender, for real
// DomKey generated on each platform please refer to individual tests.
// e.g. platform_key_map_win_unittest.cc, web_input_event_builders_mac_unittest.mm
test(function() {
    if (!window.eventSender) {
        document.write('This test requires eventSender');
        return;
    }

    var lastKeyDown, lastKeyPress, lastKeyUp;
    function recordKeyEvent(event) {
        if (event.type === "keydown")
            lastKeyDown = event;
        else if (event.type === "keypress")
            lastKeyPress = event;
        else if (event.type === "keyup")
            lastKeyUp = event;
    }
    document.addEventListener('keydown', recordKeyEvent, false);
    document.addEventListener('keypress', recordKeyEvent, false);
    document.addEventListener('keyup', recordKeyEvent, false);


    function testDomKey(keyString, expectedDomKey, shouldTestKeyPress, modifiers) {
        lastKeyDown = null;
        lastKeyPress = null;
        lastKeyUp = null;
        eventSender.keyDown(keyString, modifiers);
        assert_equals(lastKeyDown.key, expectedDomKey);
        assert_equals(lastKeyUp.key, expectedDomKey);
        if (shouldTestKeyPress)
            assert_equals(lastKeyPress.key, expectedDomKey);
    }
    // Alpha&Numeric Keys
    testDomKey('A', 'A', true, ['shiftKey']);
    testDomKey('g', 'g', true);
    testDomKey('h', 'h', true);
    testDomKey('1', '1', true);
    testDomKey('(', '(', true, ['shiftKey']);

    // Modifier Keys
    testDomKey('ControlLeft', 'Control', false);
    testDomKey('ControlRight', 'Control', false);
    testDomKey('ShiftLeft', 'Shift', false);
    testDomKey('ShiftRight', 'Shift', false);
    testDomKey('AltLeft', 'Alt', false);
    testDomKey('AltRight', 'Alt', false);
    testDomKey('NumLock', 'NumLock', false);

    // Whitespace Keys
    testDomKey('Enter', 'Enter', true);
    testDomKey('Tab', 'Tab', false);
    
    // Navigation keys
    testDomKey('ArrowRight', 'ArrowRight', false);
    testDomKey('ArrowDown', 'ArrowDown', false);
    testDomKey('ArrowLeft', 'ArrowLeft', false);
    testDomKey('ArrowUp', 'ArrowUp', false);
    testDomKey('Home', 'Home', false);
    testDomKey('End', 'End', false);
    testDomKey('PageUp', 'PageUp', false);
    testDomKey('PageDown', 'PageDown', false);
    
    // Editing Keys
    testDomKey('Backspace', 'Backspace', false);
    testDomKey('Insert', 'Insert', false);
    testDomKey('Delete', 'Delete', false);
      
    // Device Keys
    testDomKey('PrintScreen', 'PrintScreen', false);
    
    // General Purpose Function Section
    testDomKey('F1', 'F1', false);
    testDomKey('F6', 'F6', false);
    testDomKey('F12', 'F12', false);
    
    // UI Keys
    testDomKey('Escape', 'Escape', false);
    testDomKey('ContextMenu', 'ContextMenu', false);
}, 'Testing synthetic DomKey for eventSender.');
</script>
